package jdbc;

import jdbc.common.InputUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

public class RegUserDemo {
    public static void main(String[] args) throws Exception {
       UserInfo userInfo = InputUtil.getInputObject(new UserInfo(),"欢迎注册！");
        System.out.println(userInfo);

        Connection connection = DBUtil.getConnection();
        Statement statement = connection.createStatement();


        String sql = "insert into userinfo (username,password,nickname,age) values ('"
                + userInfo.getUsername() + "','"   // username是字符串类型，需要单引号
                + userInfo.getPassword() + "','"   // password是字符串类型，需要单引号
                + userInfo.getNickname() + "',"    // nickname是字符串类型，需要单引号
                + userInfo.getAge() + ")";         // age是数值类型，不需要单引号

        int num = statement.executeUpdate(sql);
        System.out.println(num > 0 ? "注册成功！" : "注册失败！");

        // 关闭资源
        statement.close();
        connection.close();
    }
}

